snapp.admin.DBObjectComponent = new Class({
	Extends: snapp.admin.AdminComponent,
	
	options: {

		name: 'dbobjects', 		// required
		formURI: 'content/forms/objects/dbobject.html', 	// required
		formID: 'dbobjectform',		// required
		objectClass: SNAppDBObject,	// required
		// TODO - Overwrite with more specific permissions
		listSecPermit: 'OBJECTS', // optional
		editSecPermit: 'OBJECTS', // optional
		// edit window options - optional
		editWindowOptions: {
			width: 500,
			height: 500
		},
	
		listOptions: {
			labelProperty: 'globalname'	
		},
		
		listFilter: {
			'status':1,
			'orderBy':'globalname'
		}
		
	},
	
	templateList: null,
	
	saveHandler: function() {
		this.parent();
		$('dbobject_template_dnld').empty();
			
	},
	
	loadHandler: function() {
		this.parent();
		
		this.templateList = new SNAppTemplate({
			events: {
				list: this.templateListHandler.bind(this)
			}
		});
		this.templateList.list({
			'status': 1,
			secCheck: snapp.session.access.getSecCheck('OBJECTS')
		});	
		
		this.downloadList = new SNAppObjectFile({
			events: {
				list: this.downloadListHandler.bind(this)
			}
		});
		this.downloadList.list({
			object: this.currentObject.getProperty('id'),
			status: 1,
			secCheck: snapp.session.access.getSecCheck('OBJECTS')
		});
	},
	
	templateListHandler: function() {
		//alert(this.templateList.length);
		$('dbobject_template_sel').empty();
		this.templateList.toCheckboxList($('dbobject_template_sel'),{boxname:'templates'});
	},
	
	downloadListHandler: function() {
		$('dbobject_template_dnld').empty();
		this.downloadList.each(function(dfile) {
			dv = new Element('div',{
				styles: {
					position: 'relative'
				}
			});
			spn = new Element('span',{
				'class':'fake_a'
			}).set('text',dfile.getProperty('name'))	
			spn.addEvent('click',function(){
				this.download({
					'id': dfile.getProperty('id'),
					secCheck: snapp.session.access.getSecCheck('OBJECTS')
				});
			}.bind(dfile));
			
			dv.grab(spn);
			$('dbobject_template_dnld').grab(dv);
				
		});
	}
	
});

snapp.admin.dbobjects = new snapp.admin.DBObjectComponent();
